package testMVC_DB.controller;
import java.sql.*;

import testMVC_DB.model.Pessoa;


public class PessoaDAO {
	
	public Pessoa selectPorCod(int codigo){
		
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		Pessoa p = new Pessoa();
		
		try{
			
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "post");
			st = conn.createStatement();
			rs = st.executeQuery("SELECT * FROM test WHERE cod="+codigo+"");
			while (rs.next()){
				
				p.setCod(rs.getInt("cod"));
				p.setNome(rs.getString("nome"));
				
			}
		}catch(Exception e){
		
		e.printStackTrace();
		
		}finally{
		
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			if (st != null)
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		
		return p;
	} 
		
}
